<?php

namespace App\Http\Controllers\Course;

use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\FileUploader;
use App\Models\Activity;
use App\Models\EnrollTeacher;
use App\Models\JdCard;
use Illuminate\Support\Facades\DB;
use Excel;
use App\Models\ActivitySponsor;
class ActivityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->input('title','');
        if($title){
           $data = Activity::where('title','like',"%{$title}%")->orderBy("orderby","desc")->orderBy('id','desc')->paginate(10);
        }else{
             $data = Activity::orderBy("orderby","desc")->orderBy('id','desc')->paginate(10);
        }

        return view("activity.index",['data'=>$data,'title'=>$title]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("activity.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if($request->user()){
            $userid = $request->user()->id;
        }else{
            $userid = '';
        }
       
        $up = new FileUploader();
        $arr =  $up->upload($request,'cover_url');
        
        $data = $request->input();
        $activity = new Activity();
        $activity->title = $request->input('title');
        $activity->description = $request->input('description');
        $activity->cover_url = $arr['url'];
        $activity->link = $request->input('link');
        $activity->seo_title = $request->input('seo_title');
        $activity->seo_description = $request->input('seo_description');
        $activity->start_time = $request->input('start_time');
        $activity->end_time = $request->input('end_time');
        $activity->state = $request->input('state');
        $activity->orderBy = $request->input('orderBy');
        $activity->opertion_user_id = $userid;
        $activity->save();

        return redirect("activity/list");
       
    }

    /*
        活动编辑也
     */
    public function edit($id)
    {
        $data = Activity::where("id",$id)->first();
       // dd($data);
        return view("activity.edit",['data'=>$data]);
    }


    /**
     * 更新页
     */
    public function update(Request $request,$id)
    {
        
        $activity = Activity::where('id',$id)->first();
        if($request->user()){
            $userid = $request->user()->id;
        }else{
            $userid = '';
        }
        
        $activity->title = $request->input('title');
        $activity->description = $request->input('description');
        $activity->cover_url = $request->input('cover_url');
        $activity->link = $request->input('link');
        $activity->seo_title = $request->input('seo_title');
        $activity->seo_description = $request->input('seo_description');
        $activity->start_time = $request->input('start_time');
        $activity->end_time = $request->input('end_time');
        $activity->state = $request->input('state');
        $activity->orderBy = $request->input('orderBy');
        $activity->opertion_user_id = $userid;
        $re = $activity->save();
        if($re){
            return redirect("activity/list");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $re = Activity::where("id",$id)->delete();
        if($re){
            return redirect("activity/list");
        }
    }
    public function activity_all($aid){
        if($aid == 1){
            $data['title'] = "新年充电活动福利1";
            $data['gift'] = "健身干货课程";
            $data['maxnum'] = 0;

        }elseif($aid == 2){
            $data['title'] = "新年充电活动福利2";
            $data['gift'] = "20元现金红包";
            $data['maxnum'] = 100;

        }elseif($aid == 3){
            $data['title'] = "新年充电活动福利3";
            $data['gift'] = "10MM瑜伽垫";
            $data['maxnum'] = 50;

        }elseif($aid == 4){
            $data['title'] = "新年充电活动福利4";
            $data['gift'] = "家用多功能收腹机";
            $data['maxnum'] = 50;
        }else{
            $data['title'] = "新年充电活动福利5";
            $data['gift'] = "价值100元京东卡";
            $data['maxnum'] = 100;

        }
        return $data;
    }
    //新年活动助力
    public function new_year(Request $request,$aid)
    {
        $mobile = $request->input('mobile','');
        if($mobile){
            $id = DB::table("users")->where("mobile", $mobile)->select("id")->first();
            $activity = ActivitySponsor::where("aid",$aid)->where("uid",$id->id)->orderBy("created_at","desc")->paginate(15);
        }else{
            $activity = ActivitySponsor::where("aid",$aid)->orderBy("created_at","desc")->paginate(15);
        }

        $data = $this->activity_all($aid);
        //dd($activity);
        $this->ret['data'] = $data;
        $this->ret['activity'] = $activity;
        $this->ret['aid'] = $aid;
        return view("activity.new_year",$this->ret);
    }

    /**
     * 20190117
     * 查看活动数据
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showDate(Request $request){
        $page = $request->input("page", 1);

        $data = DB::table("activity_team")->where("is_get",1)->where("is_sponsor", 1)
        ->select("userid", "friend","id")
        ->orderBy("id", "desc")
        ->offset(0)
        ->limit(10)
        ->get()->toArray();
        $cur_arr = array();

        foreach($data as $one){
            $arr = array();
            $arr['id']    = $one->id;
            $arr['userid'] = $one->userid;
            $arr['friend'] = $one->friend;
            $arr['name']   = $this->userinfo($one->userid, "nickname");
            $arr['mobile'] = $this->userinfo($one->userid, "mobile");
            $ones = DB::table("activity_team")->where("userid", $one->userid)->select("userid", "friend","id")->get();
            if($ones){
                $arr['one_number']  = $ones->count();
                $two_number = 0;
                foreach($ones as $o){
                    $twos = DB::table("activity_team")->where("userid", $o->userid)
                    ->select("userid", "friend","id")->get();
                    if($twos){
                        $two_number +=$twos->count();
                    }else{
                        $two_number +=0;
                    }
                }
                $arr['two_number'] = $two_number;
            }else{
                $arr['one_number']  = 0;
            }
            echo $arr['id'].'--'.$arr['userid'].'---'.$arr['friend'].'---'.$arr['name'].'---'.$arr['mobile'].'---'.$arr['one_number'].'---'.$arr['two_number'];
            echo "<hr/>";
            
            $cur_arr[] = $arr;
            unset($arr);
        }
        echo "<pre>";
        print_r($cur_arr);
        return;
        foreach($data as $one){
            $arr = array();
            $arr['userid'] = $one->userid;
            $arr['friend'] = $one->friend;
            $arr['id']     = $one->id;
            $arr['name']   = $this->userinfo($one->userid, "nickname");
            $arr['mobile'] = $this->userinfo($one->userid, "mobile");
            $ones = DB::table("activity_team")->where("userid", $one->userid)->select("userid", "friend","id")->get()->toArray();
            if($ones){
                foreach($ones as $i){
                    $onea = array();
                    $onea['userid'] = $i->userid;
                    $onea['friend'] = $i->friend;
                    $onea['id']     = $i->id;
                    $onea['name']   = $this->userinfo($i->friend, "nickname");
                    $onea['mobile'] = $this->userinfo($i->friend, "mobile");

                    $twos = DB::table("activity_team")->where("userid", $i->userid)
                            ->select("userid", "friend")->get()->toArray();
                        if($twos){

                            foreach($twos as $two){

                                $twoa = array();
                                $twoa['userid'] = $two->userid;
                                $twoa['friend'] = $two->friend;
                                $twoa['name']   = $this->userinfo($two->friend, "nickname");
                                $twoa['mobile'] = $this->userinfo($two->friend, "mobile");
                                $onea['children'][] = $twoa;
                                unset($twoa);
                            }
                        }else{
                             $onea['children'] = "--";
                        }    

                    $arr['children'][] = $onea;
                    unset($onea);
                }
            }else{
                $arr['children'] = '---';
            }
            $cur_arr[] = $arr;
            unset($arr);
        }
        echo "<pre>";
        print_r($cur_arr);
        return;
        foreach($data as $item){
            $ones = DB::table("activity_team")->where("userid", $item->userid)->select("userid", "friend")->get()->toArray();
            //dd($ones);
            if($ones){
                $ones_arr = array();
                foreach($ones as $i){
                    $one['userid'] = $i->userid;
                    $one['friend'] = $i->friend;
                    $one['name']   = $this->userinfo($i->userid, "nickname");
                    $one['mobile'] = $this->userinfo($i->userid, "mobile");
                    $ones_arr[]    = $one;
                }
                $cur_arr[] = $ones_arr;
                unset($ones_arr);
            }
            echo "<pre>";
            print_r($ones);
            print_r($cur_arr);
            // $i['userid'] = $item->userid;
            // $i['friend'] = $item->friend;
        }

    }

    protected function userinfo($id,$field){
        $user = DB::table("users")->where("id", $id)->select($field)->first();
        if($user){
            return $user->$field ? $user->$field : "暂无信息";
        }else{
            return "暂无信息";
        }
    }


    /*20190121
     * 招生老师
     */
    public function jdCard(Request $request){
        $name    = $request->input('name','');
        $jdcards = new JdCard();
        if($name){
            $jdcards = $jdcards->where('name','like','%'.$name.'%');
        }
        $jdcards = $jdcards->select('id','card_code','card_pass', "user_id", "updated_at")->orderBy('id','desc')->paginate(10);

        return view('activity.zsTeacher',['list'=>$jdcards,'name'=>$name]);
    }

    /*20190121
     * 导入招生老师信息
     */
    public function addJdCard(Request $request){
        $filePath = public_path().'/jdcard.xls';
        Excel::load($filePath, function($reader) {
            $data = $reader->all();
            $jdcards = new JdCard();
            foreach($data as $list){
                foreach($list  as $item){
                    $card = JdCard::where('card_code',$item->card_code)->select('id')->first();
                    if($card){
                        continue;
                    }
                    $jdcards = new JdCard();
                    $jdcards->card_code = $item->card_code;
                    $jdcards->card_pass = $item->card_pass;
                    $jdcards->save();
                }
            }
        });
        $this->getSessionInfo($request,'success','导入成功');
        return redirect('/activity/jdcard');

    }
}
